package com.fare.taluo.core.domain.system.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.dayatang.querychannel.service.QueryChannelService;
import com.dayatang.querychannel.support.Page;
import com.fare.taluo.core.domain.Convert;
import com.fare.taluo.core.domain.queue.message.QueueEmailMessageEntity;
import com.fare.taluo.core.domain.system.EmailEntity;
import com.fare.taluo.core.domain.system.EmailEntityRepository;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.StringAndDateAndFileUtil;
@Repository
public class EmailEntityRepositoryImpl  extends EntityRepositoryHibernate implements EmailEntityRepository{

	@Autowired
	public  QueryChannelService queryChannel1;
	private static String LOCAL_PATH;//配置文件取出的地址
	
	@Override
	public List<EmailEntity> findAllEmail(int statu) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer("from EmailEntity fm where 1=1");
		if(statu >-1){
			sbf.append(" and fm.statu= ? ");
			list.add(statu);
		}
		
		sbf.append(" order by fm.statu desc ");
		List<EmailEntity> lists=super.find(sbf.toString(),list.toArray(),EmailEntity.class);
		
		return lists;
	}

	
	@Override
	public Page<QueueEmailMessageEntity> findEntityByPage(QueueEmailMessageEntity query,int pageNo, int pageSize){
		
		List<QueueEmailMessageEntity> listEntity=new ArrayList<QueueEmailMessageEntity>();
		StringBuffer hql = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		hql.append(" from QueueEmailMessageEntity t where 1=1 ");
		if(query!=null){
			
		}
		hql.append(" order by t.createTime desc ");
		
		Page<QueueEmailMessageEntity> page=queryChannel1.queryPagedResultByPageNo(hql.toString(), params.toArray(), pageNo, pageSize);
		if(page.getTotalCount() > 0){
			for(QueueEmailMessageEntity entity : page.getResult()){
				listEntity.add(Convert.convertQueueEmailMessageEntityList(entity));
			}
		}
		int start = (pageNo - 1)*pageSize;
		return new Page<QueueEmailMessageEntity>(start, page.getTotalCount(), pageSize, listEntity);
	}

	@Override
	public List<QueueEmailMessageEntity> findEntityByDate(Date createDate) {
		StringBuffer hql = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		Date tomDate=DateUtils.strToDate(DateUtils.formatFutureDay(createDate, "yyyy-MM-dd", 1),"yyyy-MM-dd");
		hql.append(" from QueueEmailMessageEntity t where 1=1 ");
		if(createDate !=null){
			hql.append(" and t.createTime > ? ");
			hql.append(" and t.createTime < ? ");
			params.add(createDate);
			params.add(tomDate);
		}
		hql.append(" order by t.createTime desc ");
		List<QueueEmailMessageEntity> lists=super.find(hql.toString(),params.toArray(),QueueEmailMessageEntity.class);
		return lists;
	}
	
}
